const shadowRoot = document.querySelector("#app > div > div.app_content > div:nth-child(4) > micro-app").shadowRoot;

function setvalue(name,value) {
    return new Promise((resolve) => {
        // 找到所有包含 品牌 的 span.item-title-text 元素
        itemTitleTexts = shadowRoot.querySelectorAll('.input_title,.box_title');
        for (let itemTitleText of itemTitleTexts) {
            if (itemTitleText.innerText.includes(name)) {
                // 找到其父级 div.attribute-select-item 下的 .shopee-selector__inner 点击
                editRowRightMedium = itemTitleText.closest('.flex-1,.goods_details').querySelector('input[type=text],textarea');
                if (editRowRightMedium) {
                    editRowRightMedium.value=value;
                    resolve();
                    break;
                }
                editRowRightMedium = itemTitleText.closest('.flex-1').querySelector('.weui-desktop-form__dropdown__dt');
                if (editRowRightMedium) {
                    editRowRightMedium.click();
                    setTimeout(function(){
                        targetElements=shadowRoot.querySelectorAll(".weui-desktop-dropdown-menu");
                        for (let targetElement of targetElements) {
                            if (targetElement.style.display == '' &&targetElement.style.visibility == '') {
                                options=targetElement.querySelectorAll(".weui-desktop-dropdown__list-ele");
                                options.forEach(option => {
                                    let text = option.textContent;
                                    if (text.includes(value)) {
                                        option.click();
                                        resolve();
                                    }
                                });
                            }
                        };
                    },1000)
                }else{
                    resolve();
                }
            }
        };

        elements=shadowRoot.querySelectorAll('.default_box');
        for (const element of elements) {
            if (element.textContent.includes('商品参数')) {
                itemTitleTexts = element.querySelectorAll('.weui-desktop-form__input');
                for (let itemTitleText of itemTitleTexts) {
                    if(itemTitleText.getAttribute("placeholder").includes(name)){
                        itemTitleText.value=value;
                        resolve();
                    }
                }
            }
        }



    });
}